using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using HslCommunication.Core;

namespace WIT.DAQ.Core.Forms.UserControls;

/// <summary>
///
/// </summary>
internal class UC_DeviceParams_For_ModbusRtu : UserControl
{
	/// <summary>
	/// 数据解析格式
	/// </summary>
	internal string DATAFORMAT = "ABCD";

	/// <summary>
	/// 首地址是否从0开始
	/// </summary>
	internal bool? ADDRESSSTARTWITHZERO = true;

	/// <summary>
	/// 字符串是否颠倒
	/// </summary>
	internal bool? ISSTRINGREVERSE = false;

	/// <summary>
	/// 站号
	/// </summary>
	internal int STATION = 1;

	/// <summary>
	/// COM口
	/// </summary>
	internal string COMPORT = string.Empty;

	/// <summary>
	/// 波特率
	/// </summary>
	internal int BAUDRATE = 9600;

	/// <summary>
	/// 数据位
	/// </summary>
	internal int DATABITS = 8;

	/// <summary>
	/// 停止位
	/// </summary>
	internal int STOPBITS = Convert.ToInt32(StopBits.One);

	/// <summary>
	/// 奇偶校验位
	/// </summary>
	internal int PARITY = Convert.ToInt32(Parity.None);

	/// <summary> 
	/// 必需的设计器变量。
	/// </summary>
	private IContainer components = null;

	private CheckBox cbAddressStartWithZero;

	private ComboBox cmbDataFormat;

	private Label label3;

	private CheckBox cbIsStringReverse;

	private TextBox txtStation;

	private Label label1;

	private Label label2;

	private ComboBox cmbComPort;

	private Label label4;

	private TextBox txtBaudRate;

	private Label label5;

	private TextBox txtDataBits;

	private Label label6;

	private Label label7;

	private ComboBox cmbParity;

	private ComboBox cmbStopBits;

	/// <summary>
	///
	/// </summary>
	internal UC_DeviceParams_For_ModbusRtu()
	{
		InitializeComponent();
		InitDataFormat();
		InitComPort();
		InitStopBits();
		InitParity();
	}

	/// <summary>
	///
	/// </summary>
	/// <param name="dataFormat"></param>
	/// <param name="addressStartWithZero"></param>
	/// <param name="isStringReverse"></param>
	/// <param name="station"></param>
	/// <param name="comPort"></param>
	/// <param name="baudRate"></param>
	/// <param name="dataBits"></param>
	/// <param name="stopBits"></param>
	/// <param name="parity"></param>
	internal UC_DeviceParams_For_ModbusRtu(string dataFormat, bool? addressStartWithZero, bool? isStringReverse, int? station, string comPort, int? baudRate, int? dataBits, int? stopBits, int? parity)
	{
		InitializeComponent();
		InitDataFormat();
		InitComPort();
		InitStopBits();
		InitParity();
		if (dataFormat != null)
		{
			DATAFORMAT = dataFormat;
		}
		ADDRESSSTARTWITHZERO = addressStartWithZero;
		ISSTRINGREVERSE = isStringReverse;
		if (station.HasValue)
		{
			STATION = Convert.ToInt32(station);
		}
		if (dataFormat != null)
		{
			COMPORT = comPort;
		}
		if (baudRate.HasValue)
		{
			BAUDRATE = Convert.ToInt32(baudRate);
		}
		if (dataBits.HasValue)
		{
			DATABITS = Convert.ToInt32(dataBits);
		}
		if (stopBits.HasValue)
		{
			STOPBITS = Convert.ToInt32(stopBits);
		}
		if (parity.HasValue)
		{
			PARITY = Convert.ToInt32(parity);
		}
	}

	private void UC_DeviceParams_For_Modbus_Load(object sender, EventArgs e)
	{
		cmbDataFormat.SelectedValue = DATAFORMAT;
		cbAddressStartWithZero.Checked = !ADDRESSSTARTWITHZERO.HasValue || ADDRESSSTARTWITHZERO.Value;
		cbIsStringReverse.Checked = ISSTRINGREVERSE.HasValue && ISSTRINGREVERSE.Value;
		txtStation.Text = STATION.ToString();
		cmbComPort.SelectedItem = COMPORT;
		txtBaudRate.Text = BAUDRATE.ToString();
		txtDataBits.Text = DATABITS.ToString();
		cmbStopBits.SelectedValue = STOPBITS;
		cmbParity.SelectedValue = PARITY;
	}

	/// <summary>
	/// 加载数据解析格式
	/// </summary>
	private void InitDataFormat()
	{
		List<object> list = new List<object>();
		foreach (DataFormat value in Enum.GetValues(typeof(DataFormat)))
		{
			list.Add(new
			{
				Name = value.ToString(),
				Value = (int)value
			});
		}
		cmbDataFormat.DisplayMember = "Name";
		cmbDataFormat.ValueMember = "Name";
		cmbDataFormat.DataSource = list;
	}

	/// <summary>
	/// 加载串口列表
	/// </summary>
	private void InitComPort()
	{
		cmbComPort.DataSource = SerialPort.GetPortNames();
	}

	/// <summary>
	/// 加载停止位
	/// </summary>
	private void InitStopBits()
	{
		List<object> list = new List<object>();
		foreach (StopBits value in Enum.GetValues(typeof(StopBits)))
		{
			list.Add(new
			{
				Name = value.ToString(),
				Value = (int)value
			});
		}
		cmbStopBits.DisplayMember = "Name";
		cmbStopBits.ValueMember = "Value";
		cmbStopBits.DataSource = list;
	}

	/// <summary>
	/// 加载奇偶校验位
	/// </summary>
	private void InitParity()
	{
		List<object> list = new List<object>();
		foreach (Parity value in Enum.GetValues(typeof(Parity)))
		{
			switch (value.ToString())
			{
			case "None":
				list.Add(new
				{
					Name = "无",
					Value = (int)value
				});
				break;
			case "Odd":
				list.Add(new
				{
					Name = "奇",
					Value = (int)value
				});
				break;
			case "Even":
				list.Add(new
				{
					Name = "偶",
					Value = (int)value
				});
				break;
			}
		}
		cmbParity.DisplayMember = "Name";
		cmbParity.ValueMember = "Value";
		cmbParity.DataSource = list;
	}

	/// <summary>
	/// 校验输入的内容是否合法
	/// </summary>
	/// <returns></returns>
	internal bool ValidateInputs(out string msg)
	{
		msg = string.Empty;
		bool flag = false;
		try
		{
			DATAFORMAT = cmbDataFormat.SelectedValue.ToString();
			ADDRESSSTARTWITHZERO = cbAddressStartWithZero.Checked;
			ISSTRINGREVERSE = cbIsStringReverse.Checked;
			if (!int.TryParse(txtStation.Text.Trim(), out STATION))
			{
				throw new Exception("站号格式不正确");
			}
			if (cmbComPort.SelectedItem != null)
			{
				COMPORT = cmbComPort.SelectedItem.ToString();
			}
			if (!int.TryParse(txtBaudRate.Text.Trim(), out BAUDRATE))
			{
				throw new Exception("波特率格式不正确");
			}
			if (!int.TryParse(txtDataBits.Text.Trim(), out DATABITS))
			{
				throw new Exception("数据位格式不正确");
			}
			STOPBITS = (int)cmbStopBits.SelectedValue;
			PARITY = (int)cmbParity.SelectedValue;
			flag = true;
		}
		catch (Exception ex)
		{
			flag = false;
			msg = ex.Message;
		}
		return flag;
	}

	/// <summary> 
	/// 清理所有正在使用的资源。
	/// </summary>
	/// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary> 
	/// 设计器支持所需的方法 - 不要修改
	/// 使用代码编辑器修改此方法的内容。
	/// </summary>
	private void InitializeComponent()
	{
		this.cbAddressStartWithZero = new System.Windows.Forms.CheckBox();
		this.cmbDataFormat = new System.Windows.Forms.ComboBox();
		this.label3 = new System.Windows.Forms.Label();
		this.cbIsStringReverse = new System.Windows.Forms.CheckBox();
		this.txtStation = new System.Windows.Forms.TextBox();
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.cmbComPort = new System.Windows.Forms.ComboBox();
		this.label4 = new System.Windows.Forms.Label();
		this.txtBaudRate = new System.Windows.Forms.TextBox();
		this.label5 = new System.Windows.Forms.Label();
		this.txtDataBits = new System.Windows.Forms.TextBox();
		this.label6 = new System.Windows.Forms.Label();
		this.label7 = new System.Windows.Forms.Label();
		this.cmbParity = new System.Windows.Forms.ComboBox();
		this.cmbStopBits = new System.Windows.Forms.ComboBox();
		base.SuspendLayout();
		this.cbAddressStartWithZero.AutoSize = true;
		this.cbAddressStartWithZero.Checked = true;
		this.cbAddressStartWithZero.CheckState = System.Windows.Forms.CheckState.Checked;
		this.cbAddressStartWithZero.Location = new System.Drawing.Point(121, 56);
		this.cbAddressStartWithZero.Name = "cbAddressStartWithZero";
		this.cbAddressStartWithZero.Size = new System.Drawing.Size(114, 16);
		this.cbAddressStartWithZero.TabIndex = 1;
		this.cbAddressStartWithZero.Text = "起始地址从0开始";
		this.cbAddressStartWithZero.UseVisualStyleBackColor = true;
		this.cmbDataFormat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDataFormat.FormattingEnabled = true;
		this.cmbDataFormat.Location = new System.Drawing.Point(121, 30);
		this.cmbDataFormat.Name = "cmbDataFormat";
		this.cmbDataFormat.Size = new System.Drawing.Size(121, 20);
		this.cmbDataFormat.TabIndex = 0;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(32, 33);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(83, 12);
		this.label3.TabIndex = 22;
		this.label3.Text = "数据解析格式:";
		this.cbIsStringReverse.AutoSize = true;
		this.cbIsStringReverse.Location = new System.Drawing.Point(121, 78);
		this.cbIsStringReverse.Name = "cbIsStringReverse";
		this.cbIsStringReverse.Size = new System.Drawing.Size(84, 16);
		this.cbIsStringReverse.TabIndex = 2;
		this.cbIsStringReverse.Text = "字符串颠倒";
		this.cbIsStringReverse.UseVisualStyleBackColor = true;
		this.txtStation.Location = new System.Drawing.Point(121, 100);
		this.txtStation.Name = "txtStation";
		this.txtStation.Size = new System.Drawing.Size(121, 21);
		this.txtStation.TabIndex = 3;
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(80, 103);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(35, 12);
		this.label1.TabIndex = 22;
		this.label1.Text = "站号:";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(316, 33);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(41, 12);
		this.label2.TabIndex = 22;
		this.label2.Text = "COM口:";
		this.cmbComPort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbComPort.FormattingEnabled = true;
		this.cmbComPort.Location = new System.Drawing.Point(363, 30);
		this.cmbComPort.Name = "cmbComPort";
		this.cmbComPort.Size = new System.Drawing.Size(121, 20);
		this.cmbComPort.TabIndex = 0;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(310, 59);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(47, 12);
		this.label4.TabIndex = 22;
		this.label4.Text = "波特率:";
		this.txtBaudRate.Location = new System.Drawing.Point(363, 56);
		this.txtBaudRate.Name = "txtBaudRate";
		this.txtBaudRate.Size = new System.Drawing.Size(121, 21);
		this.txtBaudRate.TabIndex = 3;
		this.label5.AutoSize = true;
		this.label5.Location = new System.Drawing.Point(310, 86);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(47, 12);
		this.label5.TabIndex = 22;
		this.label5.Text = "数据位:";
		this.txtDataBits.Location = new System.Drawing.Point(363, 83);
		this.txtDataBits.Name = "txtDataBits";
		this.txtDataBits.Size = new System.Drawing.Size(121, 21);
		this.txtDataBits.TabIndex = 3;
		this.label6.AutoSize = true;
		this.label6.Location = new System.Drawing.Point(310, 113);
		this.label6.Name = "label6";
		this.label6.Size = new System.Drawing.Size(47, 12);
		this.label6.TabIndex = 22;
		this.label6.Text = "停止位:";
		this.label7.AutoSize = true;
		this.label7.Location = new System.Drawing.Point(286, 140);
		this.label7.Name = "label7";
		this.label7.Size = new System.Drawing.Size(71, 12);
		this.label7.TabIndex = 22;
		this.label7.Text = "奇偶校验位:";
		this.cmbParity.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbParity.FormattingEnabled = true;
		this.cmbParity.Location = new System.Drawing.Point(363, 137);
		this.cmbParity.Name = "cmbParity";
		this.cmbParity.Size = new System.Drawing.Size(121, 20);
		this.cmbParity.TabIndex = 0;
		this.cmbStopBits.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbStopBits.FormattingEnabled = true;
		this.cmbStopBits.Location = new System.Drawing.Point(363, 110);
		this.cmbStopBits.Name = "cmbStopBits";
		this.cmbStopBits.Size = new System.Drawing.Size(121, 20);
		this.cmbStopBits.TabIndex = 23;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.cmbStopBits);
		base.Controls.Add(this.txtDataBits);
		base.Controls.Add(this.txtBaudRate);
		base.Controls.Add(this.txtStation);
		base.Controls.Add(this.cmbParity);
		base.Controls.Add(this.cmbComPort);
		base.Controls.Add(this.cmbDataFormat);
		base.Controls.Add(this.label6);
		base.Controls.Add(this.cbIsStringReverse);
		base.Controls.Add(this.label5);
		base.Controls.Add(this.cbAddressStartWithZero);
		base.Controls.Add(this.label7);
		base.Controls.Add(this.label4);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.label3);
		base.Name = "UC_DeviceParams_For_ModbusRtu";
		base.Size = new System.Drawing.Size(523, 165);
		base.Load += new System.EventHandler(UC_DeviceParams_For_Modbus_Load);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
